package com.fmsz.physicalheadline.fragments;


import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.adapter.head.HeadViewPageAdapter;
import com.fmsz.physicalheadline.databinding.FragmentBaseHeadlineBinding;
import com.fmsz.physicalheadline.fragments.head.DynamicFragment;
import com.fmsz.physicalheadline.fragments.head.HeadFragment;
import com.fmsz.physicalheadline.interfaces.SlidingOpen;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseHeadlineFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private FragmentBaseHeadlineBinding bind;
    private SlidingOpen slidingOpen;

    private HeadFragment headFragment;
    private DynamicFragment dynamicFragment;
    private List<Fragment> fragmentList;
    private HeadViewPageAdapter adapter;

    public BaseHeadlineFragment() {
    }

public static BaseHeadlineFragment newInstance() {

    Bundle args = new Bundle();

    BaseHeadlineFragment fragment = new BaseHeadlineFragment();
    fragment.setArguments(args);
    return fragment;
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        slidingOpen = (SlidingOpen)getActivity();
        View view = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_base_headline, container, false).getRoot();
        bind = DataBindingUtil.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bind.imageTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingOpen.isOpenOrClose();
            }
        });
        bind.radioGroup.setOnCheckedChangeListener(this);

        headFragment = new HeadFragment();
        dynamicFragment = new DynamicFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(headFragment);
        fragmentList.add(dynamicFragment);
        adapter = new HeadViewPageAdapter(getFragmentManager(),fragmentList);
        bind.viewPageHead.setAdapter(adapter);
        bind.viewPageHead.setCurrentItem(0);
        bind.viewPageHead.addOnPageChangeListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.tou:
                textColorChange(0);
                bind.viewPageHead.setCurrentItem(0);
                group.setBackground(getResources().getDrawable(R.drawable.ic_switch_left));
                break;
            case R.id.dong:
                textColorChange(1);
                bind.viewPageHead.setCurrentItem(1);
                group.setBackground(getResources().getDrawable(R.drawable.ic_switch_right));
                break;
        }
    }

    //文字颜色的变化
    public void textColorChange(int position) {
        for (int i = 0; i < bind.radioGroup.getChildCount(); i++) {
            RadioButton button = (RadioButton) bind.radioGroup.getChildAt(i);
            button.setTextColor(Color.RED);
            if (position == i) {
                button.setTextColor(Color.WHITE);
            }
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==0){
            bind.radioGroup.check(R.id.tou);
        }else {
            bind.radioGroup.check(R.id.dong);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        slidingOpen = null;
    }
}
